let requestUrl;

function addHandler() {
	let type = $("#_type_").combobox("getValue");

	if(type) {
		$("#type").combobox("setValue", type);
	}

	requestUrl = "/equipment_category/insert";

	$("#category_dialog").dialog("open");
}

function editHandler() {
	let data = $("#equipment_list").datagrid("getSelected");

	if(data) {
		requestUrl = "/equipment_category/updateById";

		$("#id").val(data.id);
		$("#name").textbox("setValue", data.name);
		$("#note").textbox("setValue", data.note);
		$("#type").combobox("setValue", data.type);
		$("#grade").numberspinner("setValue", data.grade);

		$("#category_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function viewHandler() {
	let rowData = $("#equipment_list").datagrid("getSelected");

	if(rowData) {
		$("#property_list").datagrid("reload", {
			equipmentId: rowData.id
		});

		$("#detail_image").attr("src", rowData.image);
		$("#detail_name").html(rowData.name);

		ajaxGet("/equipment_type/selectById", {
			id: rowData.type
		}, function (resp) {
			let data = resp.data;

			$("#detail_typename").html(data.name);
		}, error);

		$("#detail_dialog").dialog("open");
	} else {
		unselected();
	}
}

function imageHandler() {
	let data = $("#equipment_list").datagrid("getSelected");

	if(data) {
		$("#categoryId").val(data.id);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

/**
 * 装备打造
 * @param forgeType 打造类型
 */
function forge(forgeType) {
	let rowData = $("#equipment_list").datagrid("getSelected");

	if(rowData) {
		ajaxPost("/equipment/forge", {
			equipmentId: rowData.id,
			forgeType: forgeType
		}, function (resp) {
			showMsg(resp.message);
		}, error);
	} else {
		unselected();
	}
}

$(document).ready(function() {

	$("#_type_").combobox({
		url: "/equipment_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		prompt: "-装备类型-",
		panelHeight: "auto",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#_grade_").numberspinner({
		min: 10,
		max: 100,
		width: 120,
		increment: 10,
		prompt: "装备等级"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let grade = $("#_grade_").numberspinner("getValue");
		let type = $("#_type_").combobox("getValue");

		$("#equipment_list").datagrid("reload", {
			grade: grade,
			type: type
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 120,
		required: true
	});

	$("#type").combobox({
		url: "/equipment_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#grade").numberspinner({
		min: 1,
		max: 100,
		width: 120,
		required: true
	});

	$("#note").textbox({
		width: 520,
		height: 100,
		required: true,
		multiline: true
	});

	$("#category_dialog").dialog({
		title: "装备类型",
		width: 640,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#category_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#category_dialog").dialog("close");
						$("#equipment_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#category_dialog").dialog("close");
				$("#category_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#equipment_list", "/equipment_category/upload");

	fetchUploadDialog("/equipment_category/updateImageById", function () {
		$("#equipment_list").datagrid("reload");
	});

	/******************************************************************************************************/

	$("#property_list").datagrid({
		url: "/equipment_view_attribute/selectAttributes",
		method: "get",
		width: 366,
		height: 150,
		columns: [[
			{field: "propertyName", title: "属性类别", width: 90},
			{field: "普通打造", title: "普通打造", align: "center", width: 90,
				formatter: function(value) {
					if (value) {
						return "<span>+" + value.min + "~" + value.max +"</span>";
					} else {
						return "<span>无</span>"
					}
				}
			},
			{field: "强化打造", title: "强化打造", align: "center", width: 90,
				formatter: function(value) {
					if (value) {
						return "<span>+" + value.min + "~" + value.max +"</span>";
					} else {
						return "<span>无</span>"
					}
				}
			},
			{field: "专属装备", title: "专属装备", align: "center", width: 90,
				formatter: function(value) {
					if (value) {
						return "<span>+" + value.min + "~" + value.max +"</span>";
					} else {
						return "<span>无</span>"
					}
				}
			}
		]]
	});

	$("#forge_pt").linkbutton({
		text: "&emsp;&emsp;普通打造&emsp;&emsp;"
	}).click(function () {
		forge(1);
	});

	$("#forge_qh").linkbutton({
		text: "&emsp;&emsp;强化打造&emsp;&emsp;"
	}).click(function () {
		forge(2);
	});

	$("#detail_dialog").dialog({
		title: "装备预览"
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#equipment_list").datagrid({
		url: "/equipment_category/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-search",
			text: "预览",
			handler: function() {
				viewHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "id", title: "编号", align: "center"},
			{field: "name", title: "装备名称", align: "center", width: 50},
			{field: "type", title: "装备类型", align: "center", width: 50
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/equipment_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#type_" + rowIndex).html(data.name);
					}, error);

					return "<div class='blue' id='type_" + rowIndex + "' ></div>";
				}
			},
			{field: "grade", title: "等级", align: "center", width: 50,
				formatter: function(value) {
					return "<div>" + value + "级</div>";
				}
			},
			{field: "image", title: "图片", align: "center", width: 50,
				formatter: function(value) {
					return "<img height='" + size + "' src='" + value + "' />";
				}
			},
			{field: "note", title: "说明", align: "center", width: 200,
				formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			}
		]]
	});

});